package com.module.base.base

import androidx.annotation.CallSuper
import androidx.annotation.IdRes
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.module.base.base.loadsir.LoadSirRegister
import com.module.base.base.loadsir.LoadSirReloadListener
import com.module.base.network.exception.ServerException
import com.module.base.network.exception.emptyException
import com.jaeger.library.StatusBarUtil
import com.kingja.loadsir.core.LoadService
import com.module.frame.base.BaseVmVdbFragment
import com.module.skin.SkinUtils
import com.module.skin.bean.ISkinType

/**
 *   主要用于  路由跟 、缺省图 初始化
 */
abstract class BaseVmDbFragment<VM : BaseExceptionViewModel, DB : ViewDataBinding> :
    BaseVmVdbFragment<VM, DB>(), LoadSirReloadListener {

    @JvmField
    var loadService: LoadService<Any>? = null

    /**
     * 设置缺省图
     */
    @IdRes
    protected open fun getErrorViewRes(): Int {
        return 0
    }

    protected open fun setStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(activity, 0, null) //状态栏透明
        if (SkinUtils.isUse(ISkinType.IName.DEFAULT)) {
            StatusBarUtil.setLightMode(activity)
        } else {
            StatusBarUtil.setDarkMode(activity)
        }
    }

    @CallSuper
    override fun initView() {
        super.initView()
        setStatusBar()
        ARouter.getInstance().inject(this)
        if (isErrorView()) {
            //不要使用懒加载去初始化，因为里面有Handler().post导致初始化慢问题
            loadService = LoadSirRegister.register(this, mRootView.findViewById(getErrorViewRes()))
        }
    }

    /**
     * 是否有设置缺省图
     */
    private fun isErrorView(): Boolean {
        return getErrorViewRes() != 0
    }

    override fun showLoading() {
        if (loadService != null && loadService?.currentCallback == getLoadingCallback()) {
            //如果当前是加载试图，则不需要启动加载框
            return
        }
        super.showLoading()
    }

    /**
     * 重新加载，,界面会先空白，设置成加载界面
     */
    @CallSuper
    override fun reload() {
        if (!isErrorView()) {
            return
        }
        loadService?.showCallback(getLoadingCallback())
    }

    /**
     * 刷新，保留现有的界面，展示出加载框
     */
    @CallSuper
    open fun refresh() {
        if (!isErrorView()) {
            return
        }
        showLoading()
    }

    open fun showSuccess() {
        if (activity == null || activity?.isFinishing == true) {
            return
        }
        hideLoading()
        if (!isErrorView()) {
            return
        }
        loadService?.showSuccess()
    }

    open fun showEmpty() {
        if (activity == null || activity?.isFinishing == true) {
            return
        }
        if (!isErrorView()) {
            return
        }
        try {
            loadService?.showWithConvertor(emptyException())
        } catch (e: Exception) {
            loadService?.showCallback(getEmptyCallback())
        }
    }

    open fun showError(exception: ServerException?) {
        if (activity == null || activity?.isFinishing == true) {
            return
        }
        if (!isErrorView()) {
            return
        }
        try {
            loadService?.showWithConvertor(exception)
        } catch (e: Exception) {
            loadService?.showCallback(getErrorCallback())
        }
    }
}